//
//  PPStickerTextView.m
//  PPStickerKeyboard
//
//  Created by yu on 2019/4/9.
//  Copyright © 2019年 jpas. All rights reserved.
//

#import "PPStickerInputView.h"
#import "PPStickerKeyboard.h"
#import "StickerKeyboard.h"

#import "PPUtil.h"

//static CGFloat const PPStickerTextViewHeight = 44.0;

static CGFloat const PPStickerTextViewTextViewTopMargin = 10.0;
static CGFloat const PPStickerTextViewTextViewUnfocusLeftRightPadding = 5.0;
static CGFloat const PPStickerTextViewTextViewLeftRightPadding = 16.0;
static CGFloat const PPStickerTextViewTextViewBottomMargin = 10.0;
static NSUInteger const PPStickerTextViewMaxLineCount = 6;
static NSUInteger const PPStickerTextViewMinLineCount = 2;
static CGFloat const PPStickerTextViewLineSpacing = 5.0;
static CGFloat const PPStickerTextViewFontSize = 16.0;

static CGFloat const PPStickerTextViewEmojiToggleHeight = 44.0;
static CGFloat const PPStickerTextViewToggleButtonLength = 24.0;

static CGFloat const PPStickerTextViewSendButtonWidth = 60.0;
static CGFloat const PPStickerTextViewSendButtonHeight = 35.0;

@interface PPStickerInputView () <UITextViewDelegate, PPStickerKeyboardDelegate,StickerKeyboardDelegate>

//@property (nonatomic, strong) PPStickerTextView *textView;
@property (nonatomic, strong) UIView *separatedLine;
@property (nonatomic, strong) PPButton *emojiToggleButton;
@property (nonatomic, strong) PPButton *sendButton;
@property (nonatomic, strong) PPStickerKeyboard *stickerKeyboard;
@property (nonatomic, strong) StickerKeyboard *stickerKeyboards;
@property (nonatomic, strong) UIView *bottomBGView;     // 消除语音键盘的空隙

@property (nonatomic, assign, readwrite) PPKeyboardType keyboardType;
@property (nonatomic, assign) BOOL keepsPreModeTextViewWillEdited;

@end

@implementation PPStickerInputView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentMode = UIViewContentModeRedraw;
        self.exclusiveTouch = YES;
        self.backgroundColor = [UIColor whiteColor];

        _keyboardType = PPKeyboardTypeSystem;
        _keepsPreModeTextViewWillEdited = YES;
        
        [self addSubview:self.sendButton];
        [self addSubview:self.textView];
        [self addSubview:self.separatedLine];
        [self addSubview:self.emojiToggleButton];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
//        _stickerKeyboards = [[StickerKeyboard alloc] init];
//        _stickerKeyboards.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), [self.stickerKeyboards heightThatFits]);
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    [[UIColor pp_colorWithRGBString:@"#D2D2D2"] setStroke];

    CGContextSaveGState(context);
    CGContextSetLineWidth(context, PPOnePixelToPoint());
    CGContextMoveToPoint(context, 0, PPOnePixelToPoint() / 2);
    CGContextAddLineToPoint(context, CGRectGetMaxX(self.bounds), PPOnePixelToPoint() / 2);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.textView.frame = [self frameTextView];
    self.separatedLine.frame = [self frameSeparatedLine];
    self.emojiToggleButton.frame = [self frameEmojiToggleButton];
    self.sendButton.frame = [self frameSendButton];

    [self refreshTextUI];
}

- (CGFloat)heightThatFits
{
    CGFloat textViewHeight = [self.textView.layoutManager usedRectForTextContainer:self.textView.textContainer].size.height;
    CGFloat minHeight = [self heightWithLine:PPStickerTextViewMinLineCount];//初始高度
    CGFloat maxHeight = [self heightWithLine:PPStickerTextViewMaxLineCount];
    CGFloat calculateHeight = MIN(maxHeight, MAX(minHeight, textViewHeight));
    CGFloat height = PPStickerTextViewTextViewTopMargin + calculateHeight + PPStickerTextViewTextViewBottomMargin + PPStickerTextViewEmojiToggleHeight;
    return height;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    return CGSizeMake(size.width, [self heightThatFits]);
}

- (void)sizeToFit
{
    CGSize size = [self sizeThatFits:self.bounds.size];
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame) - size.height, size.width, size.height);
}

#pragma mark - public method

- (void)clearText
{
    self.textView.text = nil;
    self.textView.font = [UIFont systemFontOfSize:PPStickerTextViewFontSize];
    [self sizeToFit];
}

- (NSString *)plainText
{
    return [self.textView.attributedText pp_plainTextForRange:NSMakeRange(0, self.textView.attributedText.length)];
}

- (void)changeKeyboardTo:(PPKeyboardType)toType
{
    if (self.keyboardType == toType) {
        return;
    }

    switch (toType) {
        case PPKeyboardTypeNone:
            [self.emojiToggleButton setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
            self.textView.inputView = nil;
            break;
        case PPKeyboardTypeSystem:
            [self.emojiToggleButton setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
            self.textView.inputView = nil;                          // 切换到系统键盘
            [self.textView reloadInputViews];                       // 调用reloadInputViews方法会立刻进行键盘的切换
            break;
        case PPKeyboardTypeSticker:            
            [self.emojiToggleButton setImage:[UIImage imageNamed:@"keyboard"] forState:UIControlStateNormal];
            self.textView.inputView = self.stickerKeyboards;         // 切换到自定义的表情键盘
            [self.textView reloadInputViews];
            break;
        default:
            break;
    }

    self.keyboardType = toType;
}

#pragma mark - getter / setter

- (PPStickerTextView *)textView
{
    if (!_textView) {
        _textView = [[PPStickerTextView alloc] initWithFrame:self.bounds];
        _textView.delegate = self;
        _textView.backgroundColor = [UIColor orangeColor];
        _textView.font = [UIFont systemFontOfSize:PPStickerTextViewFontSize];
        _textView.scrollsToTop = NO;
//        _textView.returnKeyType = UIReturnKeySend;
        _textView.enablesReturnKeyAutomatically = YES;
        _textView.layoutManager.allowsNonContiguousLayout = NO;
        _textView.placeholder = @"说说你的看法...";
        _textView.placeholderColor = [UIColor pp_colorWithRGBString:@"#B1B1B1"];
        _textView.textContainerInset = UIEdgeInsetsZero;
        if (@available(iOS 11.0, *)) {
            _textView.textDragInteraction.enabled = NO;
        }
    }
    return _textView;
}

- (UIView *)separatedLine
{
    if (!_separatedLine) {
        _separatedLine = [[UIView alloc]init];
        _separatedLine.backgroundColor = [UIColor pp_colorWithRGBString:@"#DEDEDE"];
    }
    return _separatedLine;
}

- (PPButton *)emojiToggleButton
{
    if (!_emojiToggleButton) {
        _emojiToggleButton = [[PPButton alloc] init];
        [_emojiToggleButton setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
        _emojiToggleButton.touchInsets = UIEdgeInsetsMake(-12, -20, -12, -20);
        [_emojiToggleButton addTarget:self action:@selector(toggleKeyboardDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _emojiToggleButton;
}

- (PPButton *)sendButton
{
    if (!_sendButton) {
        _sendButton = [[PPButton alloc] init];
        [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
        _sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_sendButton setTitleColor:kBaseColor forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(sendDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}

- (StickerKeyboard *)stickerKeyboards
{
    if (!_stickerKeyboards) {
        _stickerKeyboards = [[StickerKeyboard alloc] init];
        _stickerKeyboards.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), [self.stickerKeyboards heightThatFits]);
        _stickerKeyboards.delegate = self;
        _stickerKeyboard.backgroundColor = [UIColor whiteColor];
    }
    return _stickerKeyboards;
}

- (UIView *)bottomBGView
{
    if (!_bottomBGView) {
        _bottomBGView = [[UIView alloc] init];
        _bottomBGView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomBGView;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.bottomBGView.frame = CGRectMake(0, CGRectGetMaxY(frame), CGRectGetWidth(self.bounds), UIScreen.mainScreen.bounds.size.height - CGRectGetMaxY(frame));
}

- (void)setFrame:(CGRect)frame animated:(BOOL)animated
{
    if (CGRectEqualToRect(frame, self.frame)) {
        return;
    }

    void (^ changesAnimations)(void) = ^{
        [self setFrame:frame];
        [self setNeedsLayout];
    };

    if (changesAnimations) {
        if (animated) {
            [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:changesAnimations completion:nil];
        } else {
            changesAnimations();
        }
    }
}

#pragma mark - private method

- (void)refreshTextUI
{
    if (!self.textView.text.length) {
        return;
    }

    UITextRange *markedTextRange = [self.textView markedTextRange];
    UITextPosition *position = [self.textView positionFromPosition:markedTextRange.start offset:0];
    if (position) {
        return;     // 正处于输入拼音还未点确定的中间状态
    }

    NSRange selectedRange = self.textView.selectedRange;

    NSMutableAttributedString *attributedComment = [[NSMutableAttributedString alloc] initWithString:self.plainText attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:PPStickerTextViewFontSize], NSForegroundColorAttributeName: [UIColor pp_colorWithRGBString:@"#1B1B1B"] }];

    // 匹配表情
//    [PPStickerDataManager.sharedInstance replaceEmojiForAttributedString:attributedComment font:[UIFont systemFontOfSize:PPStickerTextViewFontSize]];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = PPStickerTextViewLineSpacing;
    [attributedComment addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:attributedComment.pp_rangeOfAll];

    NSUInteger offset = self.textView.attributedText.length - attributedComment.length;
    self.textView.attributedText = attributedComment;
    self.textView.selectedRange = NSMakeRange(selectedRange.location - offset, 0);
}

- (void)toggleKeyboardDidClick:(id)sender
{
    [self changeKeyboardTo:(self.keyboardType == PPKeyboardTypeSystem ? PPKeyboardTypeSticker : PPKeyboardTypeSystem)];
}

- (void)sendDidClick:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(stickerInputViewDidClickSendButton:)]) {
        [self.delegate stickerInputViewDidClickSendButton:self];
    }
}

- (CGFloat)heightWithLine:(NSInteger)lineNumber
{
    NSString *onelineStr = [[NSString alloc] init];
    CGRect onelineRect = [onelineStr boundingRectWithSize:CGSizeMake(self.textView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:PPStickerTextViewFontSize] } context:nil];
    CGFloat heigth = lineNumber * onelineRect.size.height + (lineNumber - 1) * PPStickerTextViewLineSpacing;
    return heigth;
}

- (CGRect)frameTextView
{
    CGFloat X = PPStickerTextViewTextViewUnfocusLeftRightPadding;
    CGFloat Y = PPStickerTextViewTextViewTopMargin;
    CGFloat width = self.bounds.size.width - (2 * X) - CGRectGetWidth(self.sendButton.frame);

    CGFloat height = 0;
    height = CGRectGetHeight(self.bounds) - PPStickerTextViewTextViewTopMargin - PPStickerTextViewTextViewBottomMargin - PPStickerTextViewEmojiToggleHeight;
    
    if (height < 0) {
        height = self.bounds.size.height;
    }

    return CGRectMake(X, Y, width, height);
}

- (CGRect)frameSendButton
{
    return CGRectMake(CGRectGetWidth(self.bounds) - PPStickerTextViewSendButtonWidth, CGRectGetMaxY(self.textView.frame) - PPStickerTextViewSendButtonHeight, PPStickerTextViewSendButtonWidth, PPStickerTextViewSendButtonHeight);
}

- (CGRect)frameSeparatedLine
{
    return CGRectMake(0, CGRectGetHeight(self.bounds) - PPStickerTextViewEmojiToggleHeight, self.bounds.size.width, PPOnePixelToPoint());
}

- (CGRect)frameEmojiToggleButton
{
    return CGRectMake(PPStickerTextViewTextViewLeftRightPadding, CGRectGetHeight(self.bounds) - (PPStickerTextViewEmojiToggleHeight + PPStickerTextViewToggleButtonLength) / 2, PPStickerTextViewToggleButtonLength, PPStickerTextViewToggleButtonLength);
}



#pragma mark - UITextView

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.keepsPreModeTextViewWillEdited = NO;
    [self.inputView changeKeyboardTo:PPKeyboardTypeSystem];

    if ([self.delegate respondsToSelector:@selector(stickerInputViewShouldBeginEditing:)]) {
        return [self.delegate stickerInputViewShouldBeginEditing:self];
    } else {
        return YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    [self refreshTextUI];
    
    CGSize size = [self sizeThatFits:self.bounds.size];
    CGRect newFrame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame) - size.height, size.width, size.height);
    [self setFrame:newFrame animated:NO];//如果为YES,有问题
    
    if (!self.keepsPreModeTextViewWillEdited) {
        self.textView.frame = [self frameTextView];
    }
    [self.textView scrollRangeToVisible:self.textView.selectedRange];
    
    if ([self.delegate respondsToSelector:@selector(stickerInputViewDidChange:)]) {
        [self.delegate stickerInputViewDidChange:self];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([@"\n" isEqualToString:text]) {

//        return NO;
    }

    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    self.keepsPreModeTextViewWillEdited = YES;
    CGRect inputViewFrame = self.frame;
    CGFloat textViewHeight = [self heightThatFits];
    inputViewFrame.origin.y = CGRectGetHeight(self.superview.bounds) /*- textViewHeight*/ - PP_SAFEAREAINSETS(self.superview).bottom;
    inputViewFrame.size.height = textViewHeight;
    self.frame = inputViewFrame;

    if ([self.delegate respondsToSelector:@selector(stickerInputViewDidEndEditing:)]) {
        [self.delegate stickerInputViewDidEndEditing:self];
    }
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([self.textView isFirstResponder]) {
        return YES;
    }
    return [super pointInside:point withEvent:event];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    if (!CGRectContainsPoint(self.bounds, touchPoint)) {
        if ([self isFirstResponder]) {
            [self resignFirstResponder];
        }
    } else {
        [super touchesBegan:touches withEvent:event];
    }
}

- (BOOL)isFirstResponder
{
    return [self.textView isFirstResponder];
}

- (BOOL)resignFirstResponder
{
    [super resignFirstResponder];
    self.keepsPreModeTextViewWillEdited = YES;
    [self changeKeyboardTo:PPKeyboardTypeNone];
    [self setNeedsLayout];
    return [self.textView resignFirstResponder];
}

#pragma mark - Keyboard

- (void)keyboardWillShow:(NSNotification *)notification
{
    if (!self.superview) {
        return;
    }
    
    if (!self.bottomBGView.superview) {
        [self.superview insertSubview:self.bottomBGView belowSubview:self];
    }
    
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect inputViewFrame = self.frame;
    CGFloat textViewHeight = [self heightThatFits];
    inputViewFrame.origin.y = CGRectGetHeight(self.superview.bounds) - CGRectGetHeight(keyboardFrame) - textViewHeight;
    inputViewFrame.size.height = textViewHeight;
    
    [UIView animateWithDuration:duration animations:^{
        self.frame = inputViewFrame;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    if (!self.superview) {
        return;
    }
    
    if (self.bottomBGView.superview) {
        [self.bottomBGView removeFromSuperview];
    }
    
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect inputViewFrame = self.frame;
    CGFloat textViewHeight = [self heightThatFits];
    inputViewFrame.origin.y = CGRectGetHeight(self.superview.bounds) - textViewHeight - PP_SAFEAREAINSETS(self.superview).bottom;
    inputViewFrame.size.height = textViewHeight;
    
    [UIView animateWithDuration:duration animations:^{
        self.frame = inputViewFrame;
    }];
}

#pragma mark - StickerKeyboardDelegate

- (void)stickerKeyboard:(StickerKeyboard *)stickerKeyboard didClickEmoji:(PPEmoji *)emoji
{
    if (!emoji) {
        return;
    }

    UIImage *emojiImage = [UIImage imageNamed:[@"Emoji.bundle" stringByAppendingPathComponent:emoji.imageName]];
    if (!emojiImage) {
        return;
    }

    NSRange selectedRange = self.textView.selectedRange;
    NSString *emojiString = [NSString stringWithFormat:@"[%@]", emoji.emojiDescription];
    NSMutableAttributedString *emojiAttributedString = [[NSMutableAttributedString alloc] initWithString:emojiString];
    [emojiAttributedString pp_setTextBackedString:[PPTextBackedString stringWithString:emojiString] range:emojiAttributedString.pp_rangeOfAll];

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
    [attributedText replaceCharactersInRange:selectedRange withAttributedString:emojiAttributedString];
    self.textView.attributedText = attributedText;
    self.textView.selectedRange = NSMakeRange(selectedRange.location + emojiAttributedString.length, 0);

    [self textViewDidChange:self.textView];
}

- (void)stickerKeyboardDidClickDeleteButton:(StickerKeyboard *)stickerKeyboard
{
    // 获取到当前光标的位置
    NSRange selectedRange = self.textView.selectedRange;
//    PPLog(@"length = %lu, location = %lu",(unsigned long)selectedRange.length,selectedRange.location);
    if (selectedRange.location == 0 && selectedRange.length == 0) {
        return;
    }
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
    
    NSString *text = self.textView.text;
    NSInteger stringLength = text.length;
//    PPLog(@"删除前的文字 = %@",text);
    /*查找并返回字符串中给定字符串第一次出现的范围
     - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
     */
    if ([text hasSuffix:@"]"]) {
        NSString *string;
        //substringFromIndex 索引到末尾
        //substringToIndex 从索引到给定的索引
        if (stringLength > 0) {// 我很好[微笑][色]]
            if ([@"]" isEqualToString:[text substringFromIndex:stringLength - 1]]) {
                if ([text rangeOfString:@"["].location == NSNotFound){
                    self.textView.text = [text substringToIndex:stringLength - 1];
                } else {
                    
                    self.textView.text = [text substringToIndex:[text rangeOfString:@"[" options:NSBackwardsSearch].location];
                }
            } else {
                string = [text substringToIndex:stringLength - 1];
            }
        }
        
        PPLog(@"text  ====== %@",text);
        PPLog(@"string ===== %@",string);
        
//        for (NSInteger i = text.length - 1; i >= 0; i --) {
//            char c = [text characterAtIndex:i];
//            [self.textView deleteBackward];
//            if (c == '[') {
//                break;
//            }
////            PPLog(@"删除后的文字 = %@",text);
//        }
    }
    
//    if (selectedRange.length > 0) {
//        [attributedText deleteCharactersInRange:selectedRange];
//        self.textView.attributedText = attributedText;
//        self.textView.selectedRange = NSMakeRange(selectedRange.location, 0);
//    } else {
//        NSUInteger deleteCharactersCount = 1;
//
//        // 下面这段正则匹配是用来匹配文本中的所有系统自带的 emoji 表情，以确认删除按钮将要删除的是否是 emoji。这个正则匹配可以匹配绝大部分的 emoji，得到该 emoji 的正确的 length 值；不过会将某些 combined emoji（如 👨‍👩‍👧‍👦 👨‍👩‍👧‍👦 👨‍👨‍👧‍👧），这种几个 emoji 拼在一起的 combined emoji 则会被匹配成几个个体，删除时会把 combine emoji 拆成个体。瑕不掩瑜，大部分情况下表现正确，至少也不会出现删除 emoji 时崩溃的问题了。
//        NSString *emojiPattern1 = @"[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900-\\U0001F9FF]";
//        NSString *emojiPattern2 = @"[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF]\\uFE0F";
//        NSString *emojiPattern3 = @"[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF][\\U0001F3FB-\\U0001F3FF]";
//        NSString *emojiPattern4 = @"[\\rU0001F1E6-\\U0001F1FF][\\U0001F1E6-\\U0001F1FF]";
//        NSString *pattern = [[NSString alloc] initWithFormat:@"%@|%@|%@|%@", emojiPattern4, emojiPattern3, emojiPattern2, emojiPattern1];
//        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:kNilOptions error:NULL];
//        NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:attributedText.string options:kNilOptions range:NSMakeRange(0, attributedText.string.length)];
//        for (NSTextCheckingResult *match in matches) {
//            if (match.range.location + match.range.length == selectedRange.location) {
//                deleteCharactersCount = match.range.length;
//                break;
//            }
//        }
//
//        [attributedText deleteCharactersInRange:NSMakeRange(selectedRange.location - deleteCharactersCount, deleteCharactersCount)];
//        self.textView.attributedText = attributedText;
//        self.textView.selectedRange = NSMakeRange(selectedRange.location - deleteCharactersCount, 0);
//    }

    [self textViewDidChange:self.textView];
}

//- (void)stickerKeyboardDidClickSendButton:(PPStickerKeyboard *)stickerKeyboard
//{
//    if (self.delegate && [self.delegate respondsToSelector:@selector(stickerInputViewDidClickSendButton:)]) {
//        [self.delegate stickerInputViewDidClickSendButton:self];
//    }
//}

@end
